package ModelLayer;

import java.util.*;
public class EmployeeContainer
{
    private ArrayList<Employee>  employeeContainer;
    private static EmployeeContainer instance;

    private EmployeeContainer()
    {
        employeeContainer = new  ArrayList<Employee>();
    }

    public static EmployeeContainer getInstance(){
        if (instance == null){
            instance = new EmployeeContainer();
        }
        return instance;
    }

    public Employee findEmployeeByName(String name){
        Employee pObject = null;
        int index = 0;
        boolean found = false;
        while(index < employeeContainer.size() && !found)
        {
            pObject = employeeContainer.get(index); 
            if(pObject.getName().equals(name))
            {
                found = true;
            }
            else{
                index++;
            }
        }
          if(found){
           return employeeContainer.get(index) ;
        }else{
            return  null;
        }
    }
    
    public Employee findEmployeeById(int id){
        Employee pObject = null;
        int index = 0;
        boolean found = false;
        while(index < employeeContainer.size() && !found)
        {
            pObject = employeeContainer.get(index); 
            if(pObject.getId() == id)
            {
                found = true;
            }
            else{
                index++;
            }
        }
        if(found){
           return employeeContainer.get(index) ;
        }else{
            return  null;
        }
    }
    
    public void addEmployee(Employee p){
        employeeContainer.add(p);
    }

    public void deleteEmployee(Employee p){
        if(employeeContainer.contains(p))
            employeeContainer.remove(p);
    }
    
    public ArrayList<Employee> getEmployeesList()
    {
        return employeeContainer;
    }
}
